package com.atguigu.gmall.wms.mapper;

import com.atguigu.gmall.wms.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 * 
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2022-04-18 15:28:22
 */
@Mapper
public interface WareSkuMapper extends BaseMapper<WareSkuEntity> {
    /*
    # 查询wms_ware_sku表中的 sku_id=3，库存数量满足120个的仓库
    SELECT * FROM wms_ware_sku WHERE sku_id = 3 AND stock-stock_locked >=120;
    # 锁定库存
    UPDATE wms_ware_sku SET stock_locked = stock_locked+ 120 WHERE id = 1;
    # 释放库存
    UPDATE wms_ware_sku SET stock_locked = stock_locked- 120 WHERE id = 1;
     */
    @Select("SELECT * FROM wms_ware_sku WHERE sku_id = #{skuId} AND stock-stock_locked >= #{count} ")
    List<WareSkuEntity> checkStock(@Param("skuId")Long skuId , @Param("count")Integer count);
    @Update("UPDATE wms_ware_sku SET stock_locked = stock_locked+ #{count} WHERE id = #{wareSkuId}")
    boolean lockStock(@Param("wareSkuId")Long wareSkuId , @Param("count")Integer count);
    @Update("UPDATE wms_ware_sku SET stock_locked = stock_locked- #{count} WHERE id = #{wareSkuId}")
    boolean unlockStock(@Param("wareSkuId")Long wareSkuId , @Param("count")Integer count);

}
